/*
 * Copyright 2013-2015 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xdtech.report.init;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.report.model.Config;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.model.DictionaryCode;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Operation;
import com.xdtech.sys.model.Params;

/**
 * 
 * @author max.zheng
 * @create 2015-4-4下午1:22:15
 * @since 1.0
 * @see
 */
public class ReportInit implements SysInitOperation{
	Log log = LogFactory.getLog(ReportInit.class);
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-4下午1:22:55
	 * @modified by
	 * @param baseService
	 */
	@Override
	public void initingToDb(BaseService<BaseModel> baseService) {
		List params = (List) baseService.findByHql("from Params where code=? and codeValue=?", "ReportInit","true");
		log.info("Report模块数据库初始化......");
		if (EmptyUtil.isEmpty(params)) {
			MenuFunction m2 = new MenuFunction();
			m2.setIconName("icon-chart-manage");
			m2.setNameCN("报表管理");
			m2.setOperationCode("report-manage");
			m2.setPageUrl(null);
			baseService.save(m2);
			
			MenuFunction m21 = new MenuFunction();
			m21.setIconName("icon-chart-config");
			m21.setNameCN("报表配置");
			m21.setOperationCode("reportconfig-manage");
			m21.setPageUrl("config.do?config");
			m21.setParent(m2);
			baseService.save(m21);
			baseService.save(new Operation("新增报表","sys:report:add",m21));
			baseService.save(new Operation("编辑报表","sys:report:edit",m21));
			baseService.save(new Operation("删除报表","sys:report:delete",m21));
			baseService.save(new Operation("刷新报表","sys:report:refresh",m21));
			
			MenuFunction m22 = new MenuFunction();
			m22.setIconName("icon-chart-read");
			m22.setNameCN("报表预览");
			m22.setOperationCode("reportread-manage");
			m22.setPageUrl("readReport.do?read");
			m22.setParent(m2);
			baseService.save(m22);
			
			Config configZhuZhuan = new Config("系统日志柱状图","<?xml version=\"1.0\" encoding=\"UTF-8\"?><report><columns><column field=\"OPERATOR\" title=\"操作人\" type=\"xAxis\"></column><column field=\"count\" title=\"日志量\" width=\"150\"></column><column field=\"ces\" title=\"测试\"></column></columns><datagrid><searchs><showsearch><item name=\"operator\" label=\"操作人\" type=\"textbox\"></item></showsearch><hidesearch></hidesearch></searchs></datagrid><highcharts><chart><!-- 图表类型配置 --><type>column</type></chart><exporting><enabled>true</enabled></exporting><credits><enabled>true</enabled><text>JMax平台</text><href></href></credits><plotOptions><series><animation>true</animation></series></plotOptions><yAxis><title><text>数量</text></title></yAxis></highcharts></report>","SELECT OPERATOR,COUNT(*) as count,10 as ces FROM sys_log GROUP BY OPERATOR <#if operator??>having OPERATOR='${operator}' </#if>");
			Config configPie = new Config("系统日志饼图","<?xml version=\"1.0\" encoding=\"UTF-8\"?><report><columns><column field=\"OPERATOR\" title=\"操作人\" type=\"xAxis\"></column><column field=\"count\" title=\"日志量\" width=\"150\"></column></columns><datagrid><searchs><showsearch></showsearch><hidesearch></hidesearch></searchs></datagrid><highcharts><chart><!-- 图表类型配置 --><type>pie</type></chart><exporting><enabled>true</enabled></exporting><credits><enabled>false</enabled><text>JMax</text><href></href></credits><plotOptions><pie><allowPointSelect>true</allowPointSelect><cursor>pointer</cursor>	<dataLabels><enabled>true</enabled><color>#000000</color>	<connectorColor>#000000</connectorColor><format>{point.name}: {point.percentage:.1f} %</format>	</dataLabels></pie></plotOptions><yAxis><title><text>数量</text></title></yAxis></highcharts></report>","SELECT OPERATOR,COUNT(*) as count FROM sys_log GROUP BY OPERATOR");
			Config configLine = new Config("系统日志折线图","<?xml version=\"1.0\" encoding=\"UTF-8\"?><report><columns><column field=\"OPERATOR\" title=\"操作人\" type=\"xAxis\"></column><column field=\"count\" title=\"日志量\" width=\"150\"></column><column field=\"ces\" title=\"测试\"></column></columns><datagrid><searchs><showsearch></showsearch><hidesearch></hidesearch></searchs></datagrid><highcharts><chart><!-- 图表类型配置 --><type>line</type></chart><exporting><enabled>true</enabled></exporting><credits><enabled>true</enabled><text>JMax平台</text><href></href></credits><plotOptions><series><animation>true</animation></series></plotOptions><yAxis><title><text>数量</text></title></yAxis></highcharts></report>","SELECT OPERATOR,COUNT(*) as count,10 as ces FROM sys_log GROUP BY OPERATOR");
			baseService.save(configZhuZhuan);
			baseService.save(configPie);
			baseService.save(configLine);
			Config configBrowerPie = new Config("访问浏览器占比", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><report><columns><column field=\"BROWSER_TYPE\" title=\"浏览器类型\" type=\"xAxis\"></column><column field=\"count\" title=\"访问量\" width=\"150\"></column></columns><datagrid><searchs><showsearch></showsearch><hidesearch></hidesearch></searchs></datagrid><highcharts><chart><!-- 图表类型配置 --><type>pie</type></chart><exporting><enabled>true</enabled></exporting><credits><enabled>false</enabled><text>JMax</text><href></href></credits><plotOptions><pie><allowPointSelect>true</allowPointSelect><cursor>pointer</cursor>	<dataLabels><enabled>true</enabled><color>#000000</color>	<connectorColor>#000000</connectorColor><format>{point.name}: {point.percentage:.1f} %</format>	</dataLabels></pie></plotOptions><yAxis><title><text>数量</text></title></yAxis></highcharts></report>","SELECT BROWSER_TYPE,COUNT(*) AS count FROM jmax_page_view GROUP BY BROWSER_TYPE");
			baseService.save(configBrowerPie);
			Config dayVisit = new Config("日访问趋势图","<?xml version=\"1.0\" encoding=\"UTF-8\"?><report><columns><column field=\"days\" title=\"日\" type=\"xAxis\"></column><column field=\"count\" title=\"访问量\" width=\"150\"></column></columns><datagrid><searchs><showsearch></showsearch><hidesearch></hidesearch></searchs></datagrid><highcharts><chart><!-- 图表类型配置 --><type>line</type></chart><exporting><enabled>true</enabled></exporting><credits><enabled>true</enabled><text>JMax平台</text><href></href></credits><plotOptions><series><animation>true</animation></series></plotOptions><yAxis><title><text>数量</text></title></yAxis></highcharts></report>","SELECT DATE_FORMAT(create_time,'%Y%m%d') days,COUNT(id) count FROM jmax_page_view GROUP BY days");
			Config weekVisit = new Config("周访问趋势图","<?xml version=\"1.0\" encoding=\"UTF-8\"?><report><columns><column field=\"weeks\" title=\"周\" type=\"xAxis\"></column><column field=\"count\" title=\"访问量\" width=\"150\"></column></columns><datagrid><searchs><showsearch></showsearch><hidesearch></hidesearch></searchs></datagrid><highcharts><chart><!-- 图表类型配置 --><type>line</type></chart><exporting><enabled>true</enabled></exporting><credits><enabled>true</enabled><text>JMax平台</text><href></href></credits><plotOptions><series><animation>true</animation></series></plotOptions><yAxis><title><text>数量</text></title></yAxis></highcharts></report>","SELECT DATE_FORMAT(create_time,'%Y%u') weeks,COUNT(id) count FROM jmax_page_view GROUP BY weeks");
			Config monthVisit = new Config("月访问趋势图","<?xml version=\"1.0\" encoding=\"UTF-8\"?><report><columns><column field=\"months\" title=\"月\" type=\"xAxis\"></column><column field=\"count\" title=\"访问量\" width=\"150\"></column></columns><datagrid><searchs><showsearch></showsearch><hidesearch></hidesearch></searchs></datagrid><highcharts><chart><!-- 图表类型配置 --><type>line</type></chart><exporting><enabled>true</enabled></exporting><credits><enabled>true</enabled><text>JMax平台</text><href></href></credits><plotOptions><series><animation>true</animation></series></plotOptions><yAxis><title><text>数量</text></title></yAxis></highcharts></report>","SELECT DATE_FORMAT(create_time,'%Y%m') months,COUNT(id) count FROM jmax_page_view GROUP BY months");
			baseService.save(dayVisit);
			baseService.save(weekVisit);
			baseService.save(monthVisit);
			
			//数据字典
			DictionaryCode dictionaryCode = new DictionaryCode("EXPORT_REPORT_TYPE","导出报表类型");
			baseService.save(dictionaryCode);
			baseService.save(new CodeValue("EXCEL","EXCEL",1,dictionaryCode));
			baseService.save(new CodeValue("PDF","PDF",2,dictionaryCode));
			baseService.save(new CodeValue("CVS","CVS",3,dictionaryCode));
			baseService.save(new CodeValue("TXT","TXT",4,dictionaryCode));
			baseService.save(new CodeValue("HTML","HTML",5,dictionaryCode));
			Params p = new Params("ReportInit", "true", "报表模块数据库初始化标记");
			baseService.save(p);
			log.info("Report模块数据库初始化......结束");
		}else {
			log.info("Report模块数据库已经初始化......完毕");
		}
		
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-4下午1:22:55
	 * @modified by
	 * @param baseService
	 */
	@Override
	public void initingToCache(BaseService<BaseModel> baseService) {
		
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-4下午4:36:16
	 * @modified by
	 * @return
	 */
	@Override
	public int getInitOrder() {
		return 3;
	}

	/* (非 Javadoc)
	 * initingToWebApplication
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToWebApplication(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToWebApplication(BaseService<BaseModel> baseService)
	{
		// TODO Auto-generated method stub
		
	}

}
